package com.common.adapter.util;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by MrWang on 2016/7/1.
 */
public class CommonHolderView {
    private SparseArray<View> mViews;
    private View mConvertView;
    private int mPosition;

    public CommonHolderView(Context context, ViewGroup parent, LayoutInflater layoutInflater, int layoutId, int position) {
        mViews = new SparseArray<View>();
        mPosition = position;
        mConvertView = layoutInflater.inflate(layoutId, parent, false);
        mConvertView.setTag(this);
    }

    public static CommonHolderView get(Context ctx, View convertView, ViewGroup parent, LayoutInflater inflater, int layoutId, int position) {
        CommonHolderView holderView = null;
        if (convertView == null) {
            holderView = new CommonHolderView(ctx, parent, inflater, layoutId, position);
        } else {
            holderView = (CommonHolderView) convertView.getTag();
            holderView.mPosition = position;
        }
        return holderView;
    }

    public <T extends View> T getView(int layoutID) {
        View view = mViews.get(layoutID);
        if (view == null) {
            view = mConvertView.findViewById(layoutID);
            mViews.put(layoutID, view);
        }
        return (T) view;
    }

    public View getConvertView() {
        return mConvertView;
    }
}
